package com.link.base.base.appinfo.service;

import com.link.base.base.appinfo.dao.mybatis.mapper.AppSuggestMapper;
import com.link.base.base.appinfo.model.AppSuggest;
import com.link.base.base.common.dao.mybatis.mapper.AttachmentMapper;
import com.link.base.base.common.model.Attachment;
import com.link.base.user.model.CoreUser;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.model.BasicModel;
import com.link.core.basic.query.Filter;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.util.UserUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 反馈
 *
 * @author ouyangzhenqiang
 * @date 2018/10/16
 */
@Service
public class AppSuggestServiceImpl extends BasicServiceImpl<AppSuggest> implements AppSuggestService {
    @Resource
    private AppSuggestMapper appSuggestMapper;
    @Resource
    private AttachmentMapper attachmentMapper;

    @Override
    public BasicMapper<AppSuggest> getBasicMapper() throws Exception {
        return appSuggestMapper;
    }

    @Override
    public List<AppSuggest> queryByExamplePage(AppSuggest entity) throws Exception {

        List<AppSuggest> list = super.queryByExamplePage(entity);
        for (AppSuggest fu : list) {
            QueryParams head = new QueryParams();
            head.addFilter(new Filter("headId", String.valueOf(fu.getId())));
            head.setPageFlag(false);
            Attachment t = (Attachment) BasicModel.transformClass(Attachment.class, head);
            List<Attachment> attachmentList = attachmentMapper.queryByExamplePage(t);
            if (attachmentList != null && attachmentList.size() > 0) {
                fu.setAttachmentList(attachmentList);
            }
        }
        return list;
    }

    @Override
    public void insert(AppSuggest entity) throws Exception {
        CoreUser user = UserUtil.getUser();
        Long id = entity.getId();
        if (id == null) {
            entity.setId(keyGenerateService.keyGenerate());
        }
        entity.setEmpId(user.getId());
        entity.setUserName(user.getFirstName());
        super.insert(entity);
    }
}
